package com.dinadi.yidi.ui.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.CouponCenterAdapter2;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.MyCouponsStateRequest;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/10/12  领券中心
 * Time: 14:29
 */
public class CouponCenterActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private ImageView img_mask_prompt;
    private ImageView img_no_comment;
    /*是否显示没有的图片*/
    private RelativeLayout layout_no_comment;
    private ListView lv_product_attribute;
    private CouponCenterAdapter2 mRefreshAdapter;
    private List<MyCouponsStateRequestEntity> dateBean = new ArrayList<>();
    private List<MyCouponsStateRequestEntity> dateBean2 = new ArrayList<>();
    private String userId, token;
    private boolean isShow = true;


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(CouponCenterActivity.this).inflate(R.layout.activity_coupon_center, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {

        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_coupon_center);
        lv_product_attribute = (ListView) view.findViewById(R.id.lv_product_attribute);
        lv_product_attribute.setDivider(null);
        mRefreshAdapter = new CouponCenterAdapter2(CouponCenterActivity.this, dateBean);
        lv_product_attribute.setAdapter(mRefreshAdapter);
        img_mask_prompt = (ImageView) view.findViewById(R.id.img_mask_prompt);
        layout_no_comment = (RelativeLayout) view.findViewById(R.id.layout_no_comment);
        img_no_comment = (ImageView) view.findViewById(R.id.img_no_comment);
    }

    @Override
    protected void initListener() {
        registerClickListener(img_mask_prompt);
        mRefreshAdapter.setOnClickListenerCallBack(new CouponCenterAdapter2.OnClickListenerCallBack() {
            @Override
            public void onClickDefault(int position, String isReceive, String isReceiveTime, String id, String isNoStock) {
/*"isReceive": "是否可用 【int】1-可领取 0-已领取",
"isReceiveTime": "是否到领取时间 【int】1-可领取 2-未到领取时间",
"isNoStock": "是否已领完 【int】1-可领取 2-已领完"*/
                if ("1".equals(isReceive)) {
                    if ("1".equals(isReceiveTime)) {
                        if ("1".equals(isNoStock)) {
                            MessageBean bean = new MessageBean();
                            bean.setToken(token);
                            bean.setUserId(userId);
                            bean.setCouponId(id);
                            CommonInterface messageInterface = new CommonInterface(CouponCenterActivity.this, receiveHttpListener);
                            messageInterface.getInterface("16026", JSON.toJSONString(bean));
                        } else {
                            ToastUtil.showToast("已经领完了");
                        }
                    } else {
                        ToastUtil.showToast("未到领取时间");
                    }
                } else {
                    ToastUtil.showToast("已经领取过了");
                }
            }
        });
    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(CouponCenterActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(CouponCenterActivity.this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        /*是否显示蒙版*/
        isShowMask();

    }

    private void isShowMask() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setType("9");
        CommonInterface messageInterface = new CommonInterface(CouponCenterActivity.this, maskHttpListener);
        messageInterface.getInterface("11011", JSON.toJSONString(bean));
    }

    @Override
    protected void clickEvent(View v) {

        switch (v.getId()) {
            case R.id.img_mask_prompt:
                img_mask_prompt.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    /*是否显示蒙版*/
    HttpListener maskHttpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                /*"是否显示[int] 1-显示 2-不现实",*/
                String isView = JsonUtil.getFieldValue(response, "isView");
                if ("1".equals(isView)) {
                    img_mask_prompt.setVisibility(View.VISIBLE);
                }
                initPullrefresh();
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
        }
    };

    private void initPullrefresh() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        CommonInterface messageInterface = new CommonInterface(CouponCenterActivity.this, httpListener);
        messageInterface.getInterface("16027", JSON.toJSONString(bean));
    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                MyCouponsStateRequest bean = JsonUtil.parseJsonToBean(response, MyCouponsStateRequest.class);
                dateBean2 = bean.getCouponList();
                if (dateBean2.size() == 0) {
                    if (isShow == false) {
                        layout_no_comment.setVisibility(View.GONE);
                    } else {
                        layout_no_comment.setVisibility(View.VISIBLE);
                        img_no_comment.setImageResource(R.drawable.img_mask_prompt);
                    }
                }
                dateBean.addAll(bean.getCouponList());
                mRefreshAdapter.notifyDataSetChanged();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };


    HttpListener receiveHttpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                ToastUtil.showToast("领取成功");
                SharePreferencesHelper.getInstance(CouponCenterActivity.this).putString("isRefreshCoupon", "is");
                dateBean.clear();
                initPullrefresh();
            } else {
                toast(errMsg);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("领取失败");
        }
    };
}
